package com.itlong.cloud.controller.old;

import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldSavePushInfoDTO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldPushListVO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldPushVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.project.smartcard.ISmartCardOldPushServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <desc>
 *      旧_一卡通推送模块。
 * <desc/>
 *
 * @createDate 2017-12-13.
 */
@RestController
@RequestMapping("/push/pushInfo")
public class SmartCardOldPushInfoController {
    private static final Logger LOG = LoggerFactory.getLogger(SmartCardOldPushInfoController.class);


    @Autowired
    ISmartCardOldPushServiceClient iSmartCardOldPushServiceClient;
     /**
      * <desc>
      *      旧_一卡通获取推送列表。
      * <desc/>
      *
      * @param  myUserName 登录账户
      * @param  token 凭证
      * @param  projectId 项目编号
      * @param  pageNo 当前页
      * @param  pageSize 每页行数
      * @return
      * @author shengen.T
      * @createDate 2017-12-13
      */
    @LoggerInfo(operateDesc = "旧_一卡通获取推送列表")
    @ResponseBody
    @RequestMapping(path = "/manage/list",method = RequestMethod.POST)
    public Message getPushInfoList(String myUserName,String token,String projectId,String pageNo,String pageSize){
        Message message;
        try{
            //判断用户名
            if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(myUserName)){
                return new Message(Code.LOGIN_ERROR);
            }
            //判断项目编号
            if(StringUtils.isBlank(projectId)){
                return new Message(Code.SDK_PROJECTID_ERR);
            }
            Page<SmartCardOldPushVO> pageVO=iSmartCardOldPushServiceClient.getPushInfoPage(projectId,pageNo,pageSize);
            List<SmartCardOldPushListVO> pushList = new ArrayList();
            if(pageVO.getRows()!=null){
                for (SmartCardOldPushVO vo:pageVO.getRows()){
                    SmartCardOldPushListVO pushVO= new SmartCardOldPushListVO();
                    pushVO.setInfoType(vo.getInfoType());
                    pushVO.setCreateTime(dateToStamp(vo.getCreateTime()));
                    pushVO.setStartTime(dateToStamp(vo.getStartTime()));
                    pushVO.setEndTime(dateToStamp(vo.getEndTime()));
                    pushVO.setInfoContent(vo.getInfoContent());
                    if(StringUtils.isNotEmpty(vo.getImgUrl())){
                        String url=vo.getImgUrl().split("\\?")[0].split("\\//")[1];
                        pushVO.setImgUrl(url.substring(url.indexOf("/")));
                    }
                    pushList.add(pushVO);
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("totalNumber",pageVO.getPage().getTotalNumber());
            map.put("pushs",pushList);
            message = new Message(Code.SUCCESS,map);
       }catch (Exception e){
            throw new DataAccessException("【旧一卡通getPage】获取推送列表失败", e);
       }
       return message;
    }

     /**
      * <desc>
      *      旧一卡通_添加推送。
      * <desc/>
      *
      * @param  saveDTO 保存参数
      * @return
      * @author shengen.T
      * @createDate 2017-12-13
      */
    @LoggerInfo(operateDesc = "旧一卡通_添加推送")
    @ResponseBody
    @RequestMapping(path = "/manage/add",method = RequestMethod.POST)
    public Message addPushInfo(SmartCardOldSavePushInfoDTO saveDTO,HttpServletRequest request){
       Message message;
        try{
            //判断用户名
            if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(saveDTO.getMyUserName())){
                return new Message(Code.LOGIN_ERROR);
            }
            //判断消息类型
            if((!"1".equals(saveDTO.getInfoType()))&&(!"2".equals(saveDTO.getInfoType()))){
                return new Message(Code.PARAM_ERR);
            }
            //判断消息内容
            if(StringUtils.isBlank(saveDTO.getInfoContent())){
                return new Message(Code.PARAM_ERR);
            }
            if(StringUtils.isNotEmpty(saveDTO.getStartTimeStr())){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                saveDTO.setStartTimeStr(sdf.format(new Date(Long.valueOf(saveDTO.getStartTimeStr()))));
            }
            if(StringUtils.isNotEmpty(saveDTO.getEndTimeStr())){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                saveDTO.setEndTimeStr(sdf.format(new Date(Long.valueOf(saveDTO.getEndTimeStr()))));
            }
            boolean isSuccess = iSmartCardOldPushServiceClient.addpushInfo(saveDTO.getProjectId(),
                                                                            saveDTO.getInfoContent(),
                                                                            saveDTO.getStartTimeStr(),
                                                                            saveDTO.getEndTimeStr() );
            if(!isSuccess){
                return new Message(Code.ERROR);
            }
            message = new Message(Code.SUCCESS);
       } catch (Exception e){
            throw new DataAccessException("【旧一卡通addPush】添加消息推送失败", e);
       }
       return message;
    }



    private long dateToStamp(String s) throws Exception{
        long ts =0;
        if(StringUtils.isBlank(s)){
            return ts;
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        ts = date.getTime();
        return ts;
    }
}
